package com.wisdytech.linkdcs.partLife.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.partLife.model.KeyPart;
import com.wisdytech.linkdcs.partLife.service.IKeyPartService;
import com.wisdytech.linkdcs.partLife.vo.KeyPartVO;
import com.wisdytech.linkdcs.system.model.SysUser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  关键备件前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/part/keyPart")
public class KeyPartController {
    /**
     * 注入备件信息iPartService
     */
    @Autowired
    @Qualifier("iKeyPartService")
    private IKeyPartService iKeyPartService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "lifeCycle/partLifeCycle/keyPart";
    }
    
    
    /**
     * 分页条件查询关键备件列表信息
     * @param partCode 备件编码
     * @param partName 备件名称
     * @param oSerialNum 备件序列号
     * @param status 状态
     * @param filter 分页参数
     * @return 关键备件管理列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listKeyPartByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<KeyPartVO> listKeyPartByFilter(String partCode, String partName,
    														String oSerialNum,String status,Filter filter) throws IOException{
    	PageInfo<KeyPart> keyparts =this.iKeyPartService.listKeyPartByFilter(partCode, partName,oSerialNum,status,filter);
        //转换为前台结果集
        List<KeyPart> keypartsList=keyparts.getList();
        List<KeyPartVO> keypartVOList=new ArrayList<KeyPartVO>();
        for(KeyPart keyPart:keypartsList){
        	keypartVOList.add(DataTransformUtil.changeKeyPartVO(keyPart));
        }
        return new StandardTableResult<KeyPartVO>(keyparts.getTotal(),keypartVOList);
    }
    
    /**
     * 保存关键备件信息
     * @param keyPart 关键备件对象
     * @param session 
     * @return 保存结果集
     */
    @RequestMapping(value = "/saveKeyPart/save.mvc")
    @ResponseBody
    public StandardResult saveKeyPart(KeyPart keyPart, HttpSession session) {
        SysUser sysUser = BaseUtil.getCurrentUser();
        String loginName = sysUser.getUsername();
        if (StringUtils.isBlank(keyPart.getPkId())) {
        	iKeyPartService.addKeyPart(keyPart, loginName);
        }else {
        	iKeyPartService.updateKeyPart(keyPart, loginName);
        }
        return  StandardResult.getSuccessResult();
    }
    
    
    /**
     * <p>Description:查询关键备件信息</p> 
     * @param pkId 关键备件id
     * @return 关键备件信息
     * @throws IOException 
     */
    @RequestMapping(value = "/getKeyPartById/get.mvc")
    @ResponseBody
    public StandardResult getKeyPartById(String pkId){
    	KeyPartVO keyPartVO = iKeyPartService.getKeyPartById(pkId);
        return StandardResult.getSuccessResult(keyPartVO);
    }
    
    /**
     * 通过id批量删除关键备件信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/delKeyPartById/del.mvc")
    @ResponseBody
    public StandardResult delKeyPartById(String pkId) {
    	iKeyPartService.delKeyPartById(pkId);
        return StandardResult.getSuccessResult();
    }
    
    /**
     * <p>Description:查询关键备件信息列表</p> 
     * @return 关键备件信息
     */
    @RequestMapping(value = "/getAllKeyPartByPartCode/get.mvc")
    @ResponseBody
    public StandardResult getAllKeyPartByPartCode(String search,String partCode){
        List<KeyPart> list = iKeyPartService.getAllKeyPartByPartCode(search,partCode);
        return StandardResult.getSuccessResult(list);
    }
    /**
     * <p>Description:查询关键备件信息列表</p> 
     * @return 关键备件信息
     */
    @RequestMapping(value = "/searchKeyPartByoSerial/get.mvc")
    @ResponseBody
    public StandardResult searchKeyPartByoSerial(String oSerialNum,String partName,Filter filter){
        List<KeyPart> list = iKeyPartService.searchKeyPartByoSerial(oSerialNum,partName,filter);
        PageInfo<KeyPart> pageInfo = new PageInfo<>(list);
        //判断是否是最后一页
        if(filter.getNum()>=pageInfo.getPages()){
            return StandardResult.getFailResult("",pageInfo.getList());
        }else{
            return StandardResult.getSuccessResult(pageInfo.getList());
        }
    }
    
    
    /**
     * <p>Description:查询关键备件信息列表</p> 
     * @param search 模糊查询
     * @param partId 备件id
     * @return 关键备件信息
     */
    @RequestMapping(value = "/getAllKeyPartByPartId/get.mvc")
    @ResponseBody
    public StandardResult getAllKeyPartByPartId(String search,String partId){
        List<KeyPart> list = iKeyPartService.getAllKeyPartByPartId(search,partId);
        return StandardResult.getSuccessResult(list);
    }
    
}

